package com.controller;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import com.bean.SysFunctionBean;
import com.common.GlobalPara;
import com.common.base.BaseController;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Ret;
import com.model.User;
import com.service.LoginService;

public class LoginController extends BaseController {
	public void index() {
		render("login.jsp");
	}
	@SuppressWarnings("unchecked")
	public void dologin() throws NoSuchAlgorithmException{
		String username = getPara("username");
		String password = getPara("password");
		User user = LoginService.service.valiLogin(username, password);
		//把用户信息放在session
		getSession().setAttribute(GlobalPara.CURRENT_USER, user);
		getSession().setAttribute("CURRENT_USER_NAME", user.getName());
		
		Ret result = LoginService.service.getPermission(user);
		
		Map<String,String> ButtonPermMap =  (Map<String,String>)result.get(GlobalPara.BUTTON_PERMAP);
		List<SysFunctionBean> menuData = (List<SysFunctionBean>)result.get("menudata");
		
		boolean devMode = JFinal.me().getConstants().getDevMode();
		
		
		getSession().setAttribute(GlobalPara.BUTTON_PERMAP, ButtonPermMap);
		getSession().setAttribute(GlobalPara.MENU_DATA, menuData);
		getSession().setAttribute(GlobalPara.DEV_MODE, devMode);
		
		renderJson("_success", true);
	}
	public void main(){
		setAttr("menudata",getSession().getAttribute(GlobalPara.MENU_DATA));
		setAttr(GlobalPara.DEV_MODE, getSession().getAttribute(GlobalPara.DEV_MODE));
		render("main.jsp");
	}
	public void logout(){
		getSession().invalidate();
		renderSuccess();
	}
	
}
